package org.hu.controller;

import org.hu.entity.pojo.User;
import org.hu.entity.vo.Result;
import org.hu.service.UserService;
import org.hu.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/auth")
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        User u = userService.getUserByUsername(user);
        if(u.getPassword().equals(password)){
            String token = JwtUtil.generateToken(username);
//            Map<String, String> result = new HashMap<>();
//            result.put("token", token);
            return Result.success("登录成功",token);
        }
       return Result.fail("用户不存在");
    }
}
